<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.keywords" placeholder="关键字/id/题目标题" style="width: 200px;" class="filter-item"
        @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.order" placeholder="排序规则" clearable style="width: 150px" class="filter-item">
        <el-option v-for="(item, index) in order" :key="index" :label="item" :value="index" />
      </el-select>
      <el-select v-model="listQuery.status" placeholder="课程状态选择" clearable style="width: 150px" class="filter-item">
        <el-option v-for="(item, index) in status" :key="index" :label="item" :value="index" />
      </el-select>
      <el-select v-model="listQuery.role" placeholder="发布者身份选择" clearable style="width: 150px" class="filter-item">
        <el-option v-for="(item, index) in role" :key="index" :label="item" :value="index" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getList">搜索</el-button>
    </div>
    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="ID" prop="id" sortable="custom" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.status ==0" style="color: blue;">待审核</span>
          <span v-if="scope.row.status ==1" style="color: green;">正常</span>
          <span v-if="scope.row.status ==2" style="color: red;">下架</span>
        </template>
      </el-table-column>
      <el-table-column label="会员id" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.member_id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会员昵称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.member_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会员手机号码" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.member_phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="介绍" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.describe }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程售价" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程发布者身份" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.role ==0">普通会员</span>
          <span v-if="scope.row.role ==1">申达人</span>
          <span v-if="scope.row.role ==2">申论讲师</span>
          <span v-if="scope.row.role ==3">申论主讲{}</span>
        </template>
      </el-table-column>
      <el-table-column label="评分" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.rank }}</span>
        </template>
      </el-table-column>
      <el-table-column label="购买人数" align="center">
        <template slot-scope="scope">

          <span v-if="scope.row.number_buy > 0">
            <el-button size="small" type="primary" @click="buy(scope.row)">{{ scope.row.number_buy }}</el-button>
          </span>
          <span v-else>0</span>

          <!-- <span>{{ scope.row.number_buy }}</span> -->
        </template>
      </el-table-column>
      <el-table-column label="收藏人数" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.number_collect }}</span>
        </template>
      </el-table-column>
      <el-table-column label="评分人数" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.number_rank }}</span>
        </template>
      </el-table-column>
      <el-table-column label="举报数" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.number_report }}</span>
        </template>
      </el-table-column>
      <el-table-column label="添加日期" align="center">
        <template slot-scope="{ row }">
          {{ _date(row.create_time) }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="250" align="center">
        <template slot-scope="scope">
          <div>
            <span v-permission="['/library/answerEdit']">
              <el-button size="small" type="primary" @click="edit(scope.row)">编 辑</el-button>
              <!-- <el-button size="small" type="primary" @click="del(scope.row)">删 除</el-button> -->
              <el-button size="small" type="primary" @click="sub_list(scope.row)">子课程</el-button>
            </span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog title="编辑课程" :visible.sync="show">
      <el-form>
        <el-form-item label="课程名称" label-width="150px">
          <el-input v-model="item.title" disabled />
        </el-form-item>
        <el-form-item label="课程售价" label-width="150px">
          <el-input v-model="item.money" />
        </el-form-item>
        <el-form-item label="课程审核" label-width="150px">
          <el-select v-model="item.status" placeholder="课程状态选择" clearable style="width: 150px" class="filter-item">
            <el-option label="待审核" :value="0" />
            <el-option label="正常" :value="1" />
            <el-option label="下架" :value="2" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handle()">确 定</el-button>
      </div>
      <!-- <el-alert title="售价如果设置为0则认为不是高分答案" type="info" center show-icon></el-alert> -->
    </el-dialog>
    <el-dialog title="购买课程记录" :visible.sync="buyShow">
      <el-table :key="tableKey" v-loading="listLoading" :data="buyList" border fit highlight-current-row style="width: 100%;">
        <el-table-column label="ID" prop="id" sortable="custom" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="会员id" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.member_id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="会员昵称" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.member_name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="会员手机号码" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.member_phone }}</span>
          </template>
        </el-table-column>
        <el-table-column label="花费金额" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.money }}</span>
          </template>
        </el-table-column>
        <el-table-column label="评价课程分" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.rank }}</span>
          </template>
        </el-table-column>
        <el-table-column label="购买日期" align="center">
          <template slot-scope="{ row }">
            {{ _date(row.create_time) }}
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="buyTotal > 0" :total="buyTotal" :page.sync="buyQuery.page" :limit.sync="buyQuery.limit"
        @pagination="buy" />
    </el-dialog>

  </div>
</template>

<script>
  import {
    courseList,
    courseEdit,
    buyList
  } from '@/api/course';
  import {
    addressList
  } from '@/api/config';
  import waves from '@/directive/waves';
  import Pagination from '@/components/Pagination';
  import permission from '@/directive/permission/index.js';
  import {
    dateHandle
  } from '@/api/dateHandle';
  import imageUpload from '@/components/Upload/image';

  export default {
    components: {
      Pagination,
      imageUpload
    },
    directives: {
      waves,
      permission,

    },
    data() {
      return {
        tableKey: 0,
        list: null,
        total: 0,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
        },
        order: {
          money: '售价',
          number_collect: '收藏数',
          number_buy: '购买数',
          number_rank: '评分数',
          rank: '好评率',
          number_report: '举报数',
          id: '最新',
        },
        status: {
          0: '待审核',
          1: '正常',
          2: '下架'
        },
        role: {
          0: '普通会员',
          1: '申达人',
          2: '申论讲师',
          3: '申论主讲'
        },

        item: {},
        show: false,
        buyShow: false,
        buyList: {},
        buyTotal: 0,
        buyQuery: {
          page: 1,
          limit: 20,
          type: 1
        },
      };
    },
    created() {
      this.getList()

    },
    methods: {
      _date(t) {
        return dateHandle('Y-m-d H:i:s', t)
      },

      buy(item) {
        if (item.id != this.buyQuery.course_id) {
          this.buyQuery.course_id = item.id
          this.buyQuery.page = 1
          this.buyQuery.limit = 20
        }
        // this.buyShow = true
        buyList(this.buyQuery).then(res => {
          this.buyList = res.data.data
          this.buyTotal = res.data.total
          this.buyShow = true
        })
      },
      sub_list(item) {
        this.$router.push({
          name: 'course/subList',
          params: {
            id: item.id,
            title: item.title
          }
        })
      },
      edit(item) {
        this.item = item
        this.show = true
      },

      handle() {
        courseEdit(this.item).then(res => {
          if (res.code == 20000) {
            this.$message({
              type: 'success',
              message: res.msg
            });
            this.show = false;
            this.getList()
          }
        })
      },
      getList() {
        this.listLoading = true;
        courseList(this.listQuery).then(res => {
          this.list = res.data.data;
          this.total = res.data.total;
          setTimeout(() => {
            this.listLoading = false;
          }, 0.3 * 1000);
        });
      },

    }
  };
</script>
